<?php

namespace app\adminapi\lists\reimbursement;

use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\reimbursement\ExpenseType;

class ExpenseTypeLists extends BaseAdminDataLists implements ListsSearchInterface
{
    /**
     * @notes 设置搜索条件
     * @return \string[][]
     */
    public function setSearch(): array
    {
        return [
            '=' => ['status'],
            '%like%' => ['name']
        ];
    }

    /**
     * @notes 获取费用类型列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function lists(): array
    {
        $list = ExpenseType::where($this->searchWhere)
            ->where('delete_time', null) // 过滤已删除
            ->field(['id', 'name', 'status', 'create_time'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();

        // 状态映射
        $statusMap = [
            0 => '启用',
            1 => '禁用'
        ];

        return array_map(function ($item) use ($statusMap) {
            $item['status_desc'] = $statusMap[$item['status']] ?? '未知状态';
            return $item;
        }, $list);
    }

    /**
     * @notes 获取数量
     * @return int
     */
    public function count(): int
    {
        return ExpenseType::where($this->searchWhere)
            ->where('delete_time', null)
            ->count();
    }
}